//引入spark-md5
self.importScripts('spark-md5.min.js')
//worker接受消息并返回hash
self.onmessage = message => {
  const chunks = message.data
  const spark = new self.SparkMD5.ArrayBuffer()
  let count = 0
  //递归计算hash
  const calculateHash = index => {
    const reader = new FileReader()
    reader.readAsArrayBuffer(chunks[index].file)
    reader.onload = function (e) {
      count++
      spark.append(e.target.result)
      if (count === chunks.length) {
        self.postMessage({
          hash: spark.end(),
          progress: 100
        })
      } else {
        self.postMessage({
          progress: ((count / chunks.length) * 100).toFixed(2) - 0
        })
        calculateHash(count)
      }
    }
  }
  calculateHash(count)
}